package com.cslc.mp.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cslc.mp.bean.message.custommessage.CustomMessage;
import com.cslc.mp.bean.message.custommessage.Text;
import com.cslc.mp.service.CoreService;
import com.cslc.mp.service.CoreServiceFactory;
import com.cslc.mp.service.CoreServiceImpl;

public class TestServlet extends HttpServlet {

	private static Logger logger = Logger
			.getLogger(TestServlet.class.getName());

	/**
	 * Constructor of the object.
	 */
	public TestServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		CoreService coreService = new CoreServiceImpl();
		JSONObject jsonObject = null;

		/*
		 * 测试获取用户基本信息 String openId = "o_LecwlsPw9mTZJukTIjLgO_yLxU"; jsonObject
		 * = coreService.userInfoGet(openId);
		 */

		/*
		 * tomcat数据库连接池DBCP的测试 Context initContext; try { initContext = new
		 * InitialContext(); Context envContext =
		 * (Context)initContext.lookup("java:/comp/env"); DataSource ds =
		 * (DataSource)envContext.lookup("jdbc/test"); Connection conn =
		 * ds.getConnection(); Statement st = conn.createStatement(); String
		 * yoursql = "select * from user"; ResultSet rs =
		 * st.executeQuery(yoursql); while(rs.next()) { logger.debug("id：" +
		 * rs.getInt("id")); logger.debug("openid：" + rs.getString("open_id"));
		 * logger.debug("终端编码：" + rs.getString("terminal_no"));
		 * logger.debug("销售员编号：" + rs.getString("saler_no")); logger.debug("密码："
		 * + rs.getString("password")); } } catch (NamingException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } catch (SQLException
		 * e) { // TODO Auto-generated catch block e.printStackTrace(); }
		 */

		// //测试获取自定义菜单的结构
		// coreService.menuGet();

		/* 测试客服接口发送消息
		CustomMessage message = new CustomMessage();
		Text text = new Text();
		String openid = "o_LecwlsPw9mTZJukTIjLgO_yLxU";
		message.setTouser(openid);
		message.setMsgtype("text");
		text.setContent("客服接口测试");
		message.setText(text);
		CoreServiceFactory.getInstance().customMessageSend(message);
		*/
		
		/*获取所有客服账号接口测试
		CoreServiceFactory.getInstance().kfListGet();
		*/
		
		//通过ticket换取二维码
		String terminalNo = "1234567890123";
		JSONObject ticketJson = CoreServiceFactory.getInstance().permTicketCreate(terminalNo);
		String ticket = ticketJson.getString("ticket");
		String url = ticketJson.getString("url");
		
		CoreServiceFactory.getInstance().qrCodeShow(ticket);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 测试获取accessToken
		// AccessTokenInfo accessToken =WeChatUtil.getAccessToken();

		// 测试自定义菜单创建
		// Menu menu = new Menu();
		// ClickButton buttonOne = new ClickButton("今日歌曲", "V1001_TODAY_MUSIC");
		// MultiButton buttonTwo = new MultiButton("菜单");
		// List<BaseButton> sub_button = new ArrayList<BaseButton>();
		// ViewButton button1 = new ViewButton("搜索", "http://www.baidu.com/");
		// ViewButton button2 = new ViewButton("视频", "http://v.qq.com/");
		// ClickButton button3 = new ClickButton("赞一下我们", "V1001_GOOD");
		// sub_button.add(button1);
		// sub_button.add(button2);
		// sub_button.add(button3);
		// buttonTwo.setSub_button(sub_button);
		// List<BaseButton> button = new ArrayList<BaseButton>();
		// button.add(buttonOne);
		// button.add(buttonTwo);
		// menu.setButton(button);
		// CoreService coreService = new CoreServiceImpl();
		// coreService.menuCreate(menu);

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {

	}

}
